<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_AUTOCOMPLETE
	'自动完成

    ' 操作状态
    Private MODEL_INSERT	'  插入模型数据
    Private MODEL_UPDATE	'  更新模型数据
    Private MODEL_BOTH		'  包含上面两种方式	
	
	Sub handle( ByRef [auto], ByRef data, ByRef mode )
	
		dim row,bound,arr,model,rule,replace_str
		If not isEmpty( [auto] ) Then
			for each row in [auto]				
				' 填充因子定义格式
                ' array('field','填充内容','填充条件','附加规则',[额外参数])
				bound = ubound(row)
				if bound < 2 Then	'默认为新增的时候自动填充
					bound = bound+1
					ReDim Preserve row(bound)
					row(bound) = 1
				End If
				if mode = row(2) OR MODEL_BOTH = row(2) Then
					if bound<3 Then
						bound = bound+1
						ReDim Preserve row(bound)
						row(bound) = "string"
					End If
					rule = trim( row(3) )
					select case rule
						case "function","callback"	'使用函数进行填充 字段的值作为参数							
							on error resume next
							if rule = "callback" then
								arr = split(row(1),".")
								model = arr(0)
								model = Replace(model,"Model","")
								set model = POP_MVC.Model( model )
								replace_str = "model." & arr(1)									
							else
								replace_str = row(1)
							end if
							
							if bound = 4 Then	'如果有分配的参数								
								if not isArray( row(4) ) Then	'如果不是数组，转化成数组再处理
									row(4) = array( row(4) )
								end If
									
								if isArray( row(4) ) Then
									if data.Exists( row(0) ) Then	'如果data中存在row(0)这个键
										if ubound(row(4)) >= 0 Then	' 如果想使用多个参数，调用方法中( data(row(0)),其他参数 )
											Execute "data(""" & row(0) & """)" & "=" &  replace_str & "( """ & data(row(0)) & ""","""  & join( row(4) , """,""" )  & """ )"
										Else '当只想使用 data(row(0))这个参数时,只需要将 row(4)设为 array()							
											Execute  "data(""" & row(0) & """)" & "=" &  replace_str & "( """ & data(row(0)) & """ )"
										end If										
									else
										if ubound(row(4)) >= 0 Then
											Execute "data.add """ & row(0) & """" & "," &  replace_str & "( "  & """"  & join( row(4) , """,""" )  & """ )"
										Else
											Execute  "data.add """ & row(0) & """" & "," &  replace_str & "( " & " )"
										end If
									End If
								End If
							Else	'如果没有分配参数，则当不使用参数								
								if data.Exists( row(0) ) Then									
									Execute "data(""" & row(0) & """)" & "=" &  replace_str
								Else 
									Execute "data.add """ & row(0) & """" & "," &  replace_str
								End If								
							End If
							call L_("")							
						case "field"	'用其它字段的值进行填充		
							
							data( row(0) ) = data( row(1) )
						case "ignore"	' 为空忽略							
							if data( row(0) ) = "" Then
								data.remove( row(0) )
							End If
						case "string"
							data( row(0) ) = row(1)
						case else
							data( row(0) ) = row(1)
					End Select
				
				end If
			next
		end if	
	End Sub
	
	Private Sub Class_Initialize
		MODEL_INSERT =1	'  插入模型数据
		MODEL_UPDATE =2	'  更新模型数据
		MODEL_BOTH	 =3	'  包含上面两种方式		
	End Sub
End Class
%>